package leetcode_2001_2100;

import node.Node;

import java.util.*;

public class LeeCode_2059 {
    public static void main(String[] args) {
//        System.out.println(minimumOperations(new int[]{2, 4, 12}, 2, 12));
        System.out.println(minimumOperations(new int[]{3,5,7}, 0, -4));
    }
    private static int minimumOperations(int[] nums, int start, int goal) {
        Deque<Integer> d = new ArrayDeque<>();
        Map<Integer, Integer> map = new HashMap<>();
        d.add(start);
        map.put(start, 0);
        while (!d.isEmpty()){
            int cur = d.pollFirst();
            int step = map.get(cur);
            for (int num : nums) {
                int[] rs = new int[]{cur + num, cur - num, cur ^ num};
                for (int r : rs) {
                    if (r == goal)
                        return step + 1;
                    if (r < 0 || r > 1000)
                        continue;
                    if (map.containsKey(r)){
                        continue;
                    }
                    d.addLast(r);
                    map.put(r, step + 1);
                }
            }
        }
        return -1;
    }

}
